package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.CurCourseVideoRelMapper;
import com.macro.mall.mapper.CurVideoMapper;
import com.macro.mall.model.CurVideo;
import com.macro.mall.portal.service.CurVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CurVideo的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class CurVideoServiceImpl implements CurVideoService {


    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

	@Autowired
	private CurVideoMapper curVideoMapper;
	@Autowired
	private CurCourseVideoRelMapper curCourseVideoRelMapper;

	@Override
	public List<CurVideo> find(CurVideo value) {
		//TODO这里可以做通过Assist做添加查询
        List<CurVideo> result = null;
        try {
            result = curVideoMapper.selectCurVideo(null);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
			log.debug("执行获取CurVideo数据集-->结果:", result);
		}
		return result;
	}



    @Override
    public List<CurVideo> findByCourseId(Long id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CurVideo> result = null;
        try {
            result = curVideoMapper.findByCourseId(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CurVideo数据集-->结果:", result);
        }
        return result;
    }


    @Override
	public CurVideo findOne(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id获得CurVideo对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过CurVideo的id获得CurVideo对象-->失败:id不能为空");
		}
		CurVideo result = curVideoMapper.selectCurVideoById(id);
		if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id获得CurVideo对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(CurVideo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将CurVideo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
        int result = 0;
        try {
            result = curVideoMapper.insertNotNullCurVideo(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
			log.debug("执行将CurVideo中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(CurVideo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->失败:对象为null");
		}
        int result = 0;
        try {
            result = curVideoMapper.updateNotNullCurVideoById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id更新CurVideo中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public CommonResult deleteById(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过CurVideo的id删除CurVideo-->失败:id不能为空");
			}
                Asserts.fail("执行通过CurVideo的id删除CurVideo-->失败:id不能为空");
		}
        int result = 0;
        try {
            result = curVideoMapper.deleteCurVideoById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
			log.debug("执行通过CurVideo的id删除CurVideo-->结果:", result);
		}
        return CommonResult.success(null);
	}


}